package fun.boomcat.luckyhe.mirai.plugin.dougu.command;

import fun.boomcat.luckyhe.mirai.plugin.dougu.service.DouguListService;
import fun.boomcat.luckyhe.mirai.plugin.dougu.service.DouguListServiceImpl;
import fun.boomcat.luckyhe.mirai.plugin.dougu.utils.DouguCommandHelp;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import net.mamoe.mirai.console.command.CommandOwner;
import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.console.command.RawCommand;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.message.data.MessageChain;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

public class DouguCommand extends RawCommand {
    public DouguCommand(@NotNull CommandOwner owner, @NotNull String primaryName, @NotNull String[] secondaryNames, @NotNull String usage, @NotNull String description, @NotNull Permission parentPermission, boolean prefixOptional) {
        super(owner, primaryName, secondaryNames, usage, description, parentPermission, prefixOptional);
    }

    @Nullable
    @Override
    public Object onCommand(@NotNull CommandSender commandSender, @NotNull MessageChain messageChain, @NotNull Continuation<? super Unit> continuation) {
        String primaryName = getPrimaryName();
        String[] secondaryNames = getSecondaryNames();

        int mcLen = messageChain.toArray().length;
        if (mcLen == 0) {
            commandSender.sendMessage(DouguCommandHelp.helpStr(primaryName, secondaryNames));
            return null;
        }

        String operation = messageChain.get(0).toString();

        switch (operation.toLowerCase()) {
            case "init":
            case "初始化":
                onInit(commandSender, primaryName, secondaryNames);
                break;
        }

        return null;
    }

    private void onInit(CommandSender commandSender, String primaryName, String[] secondaryNames) {
        DouguListService service = new DouguListServiceImpl();

        try {
            service.createDouguDB();
            commandSender.sendMessage("创建数据库完成！");
        } catch (Exception e) {
            commandSender.sendMessage("创建数据库失败：" + e.toString());
        }

        try {
            service.createDouguListTable();
            commandSender.sendMessage("创建道具表完成！");
        } catch (Exception e) {
            commandSender.sendMessage("创建道具表失败：" + e.toString());
        }
    }
}
